package com.luke.exercise.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CopyOnWriteArrayListTest {

    private static final int POOL_SIZE = 2;
    
    public static void main(String[] args) {
        List<Double> list = new CopyOnWriteArrayList<Double>();
        ExecutorService es = Executors.newFixedThreadPool(POOL_SIZE);
        es.execute(new AddThread(list));
        es.execute(new AddThread(list));
        es.shutdown();
    }
}

class AddThread implements Runnable {

    private List<Double> list;
    
    public AddThread(List<Double> list) {
        this.list = list;
    }
    
    public void run() {
       for(int i=0;i<10000;++i) {
           list.add(Math.random());
       }
    }
}
